package com.pzhyd.sc.biz.impl;

import java.util.ArrayList;
import java.util.List;

import com.pzhyd.sc.biz.inter.IareaBiz;
import com.pzhyd.sc.biz.inter.ItypeBiz;
import com.pzhyd.sc.dao.inter.IBaseDao;
import com.pzhyd.sc.entity.Roleinfo;
import com.pzhyd.sc.entity.Typeinfo;
import com.pzhyd.sc.entity.UsersInfo;
import com.pzhyd.sc.entity.Votearea;
import com.pzhyd.sc.util.Page;

/** 
 * @Project: webvote
 * @Title: AtpeBizImpl.java
 * @Package com.pzhyd.sc.biz.impl
 * @Description: TODO
 * @author Gaoshiwei 812257531@qq.com
 * @date Jul 11, 2012 4:12:05 PM
 * @Copyright: 2012 攀枝花移动 All rights reserved.
 * @version V1.0  
 */
public class AtypeBizImpl implements IareaBiz, ItypeBiz {
	
	private IBaseDao idao;
	

	public IBaseDao getIdao() {
		return idao;
	}

	public void setIdao(IBaseDao idao) {
		this.idao = idao;
	}

	public boolean deleteVVotearea(Votearea va) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.delete(va);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public boolean deleteVas(int[] vaid) {
		// TODO Auto-generated method stub
		int count=0;
		StringBuilder sbStr = new StringBuilder();
		for (int i=0; i<vaid.length; i++) {
			sbStr.append("?");
			if (i < (vaid.length - 1)) {
				sbStr.append(",");
			}
		}
		List params = new ArrayList();
		for (int i=0; i<vaid.length; i++) {
			params.add(vaid[i]);
		}
		
		 String hql = "delete from Votearea where vaId in (" + sbStr.toString()  +  ")";
		 try {
			count=idao.operateHql(hql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(count>0)
			return true;
		else
			return false;
	}

	public boolean insertVotearea(Votearea va) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.insert(va);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public boolean updateVotearea(Votearea va) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.update(va);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public boolean deleteType(Typeinfo ti) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.delete(ti);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public boolean deleteTypes(int[] tiid) {
		// TODO Auto-generated method stub
		int count=0;
		StringBuilder sbStr = new StringBuilder();
		for (int i=0; i<tiid.length; i++) {
			sbStr.append("?");
			if (i < (tiid.length - 1)) {
				sbStr.append(",");
			}
		}
		List params = new ArrayList();
		for (int i=0; i<tiid.length; i++) {
			params.add(tiid[i]);
		}
		
		 String hql = "delete from Typeinfo where tiId in (" + sbStr.toString()  +  ")";
		 try {
			count=idao.operateHql(hql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(count>0)
			return true;
		else
			return false;
	}

	public boolean insertType(Typeinfo ti) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.insert(ti);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}
	//查询出所有的类型信息并分页
	public void searchTypeByAll(Page page) {
		// TODO Auto-generated method stub
		String hql1 = "select count(*) from Typeinfo";
		String hql = "from Typeinfo ti order by ti.tiId ";
		try {
			page.setTotalRecords(idao.getTotalCount(hql1, null));
			idao.searchHql(hql, null, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean updateType(Typeinfo ti) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.update(ti);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public Typeinfo searchById(int tiid) {
		// TODO Auto-generated method stub
		Typeinfo ti=null;
		try {
			ti=(Typeinfo) idao.searchById(Typeinfo.class, tiid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ti;
	}

	public void searchAreaByAll(Page page) {
		// TODO Auto-generated method stub
		String hql1 = "select count(*) from Votearea";
		String hql = "from Votearea va order by va.vaId ";
		try {
			page.setTotalRecords(idao.getTotalCount(hql1, null));
			idao.searchHql(hql, null, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Typeinfo> searchAlltype() {
		// TODO Auto-generated method stub
		String hql = "from Typeinfo";
		List<Typeinfo> lt = null;
		try {
			lt = idao.searchHql(hql, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lt;
	}

}
